package com.beehoo.dcportal.admin.action;

import java.util.List;

import com.beehoo.dcportal.admin.service.ThemeService;
import com.beehoo.dcportal.common.Constant;
import com.beehoo.dcportal.common.base.BaseAction;
import com.beehoo.dcportal.common.base.BaseException;
import com.beehoo.dcportal.model.FileBean;
import com.beehoo.dcportal.model.ThemeBean;
import com.beehoo.dcportal.model.ThemeCodeBean;

public class ThemeAction extends BaseAction {

	private static final long serialVersionUID = -6220232034665608276L;
	private ThemeService themeService;
	private List<ThemeBean> resultList;
	private ThemeBean themeBean;
	private List<ThemeCodeBean> codeList;
	private String themeId;
	private FileBean logoFileBean;
	private FileBean bgFileBean;

	public List<ThemeBean> getResultList() {
		return resultList;
	}

	public void setResultList(List<ThemeBean> resultList) {
		this.resultList = resultList;
	}

	public ThemeBean getThemeBean() {
		return themeBean;
	}

	public void setThemeBean(ThemeBean themeBean) {
		this.themeBean = themeBean;
	}

	public String getThemeId() {
		return themeId;
	}

	public void setThemeId(String themeId) {
		this.themeId = themeId;
	}

	public List<ThemeCodeBean> getCodeList() {
		return codeList;
	}

	public void setCodeList(List<ThemeCodeBean> codeList) {
		this.codeList = codeList;
	}

	public FileBean getLogoFileBean() {
		return logoFileBean;
	}

	public void setLogoFileBean(FileBean logoFileBean) {
		this.logoFileBean = logoFileBean;
	}

	public FileBean getBgFileBean() {
		return bgFileBean;
	}

	public void setBgFileBean(FileBean bgFileBean) {
		this.bgFileBean = bgFileBean;
	}

	public void setThemeService(ThemeService themeService) {
		this.themeService = themeService;
	}

	public String list(){
		String streetId = (String) getSession().get(Constant.SESSION_STREET_ID);
		try {
			resultList = themeService.queryThemes(streetId);
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
		}
		return SUCCESS;
	}
	
	public String del(){
		String streetId = (String) getSession().get(Constant.SESSION_STREET_ID);
		try {
			themeService.delTheme(streetId, themeId);
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
		}
		return list();
	}
	
	public String initUpdate(){
		String streetId = (String) getSession().get(Constant.SESSION_STREET_ID);
		try {
			codeList = themeService.queryThemeCode();
			themeBean = themeService.queryTheme(streetId, themeId);
			getRequest().setAttribute("method", "update");
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
			return list();
		}
		return INPUT;
	}
	
	public String update(){
		String streetId = (String) getSession().get(Constant.SESSION_STREET_ID);
		try {
			if(themeBean != null){
				themeBean.setStreetId(streetId);
				themeBean.setLogoFile(logoFileBean);
				themeBean.setLogoImgDir(super.getText("custom.logo.img.dir"));
				themeBean.setLogoImgPath(super.getText("custom.logo.img.path"));
				themeBean.setBgFile(bgFileBean);
				themeBean.setBgImgDir(super.getText("custom.back.img.dir"));
				themeBean.setBgImgPath(super.getText("custom.back.img.path"));
			}
			codeList = themeService.queryThemeCode();
			themeService.updateTheme(themeBean);
		} catch (BaseException e) {
			getRequest().setAttribute("method", "update");
			setMessage(super.getText(e.getMessage()));
			return INPUT;
		}
		return list();
	}
	
	public String initCreate(){
		try {
			codeList = themeService.queryThemeCode();
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
		}
		return INPUT;
	}
	
	public String create(){
		String streetId = (String) getSession().get(Constant.SESSION_STREET_ID);
		String creatorId = (String) getSession().get(Constant.SESSION_USER_ID);
		String creator = (String) getSession().get(Constant.SESSION_USER_NAME);
		if(themeBean != null){
			themeBean.setStreetId(streetId);
			themeBean.setCreatorId(creatorId);
			themeBean.setCreator(creator);
			themeBean.setLogoFile(logoFileBean);
			themeBean.setLogoImgDir(super.getText("custom.logo.img.dir"));
			themeBean.setLogoImgPath(super.getText("custom.logo.img.path"));
			themeBean.setBgFile(bgFileBean);
			themeBean.setBgImgDir(super.getText("custom.back.img.dir"));
			themeBean.setBgImgPath(super.getText("custom.back.img.path"));
		}
		try {
			codeList = themeService.queryThemeCode();
			themeId = themeService.createTheme(themeBean);
			themeBean.setThemeId(themeId);
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
			return INPUT;
		}
		return "portlet";
	}
	
	public String use(){
		String streetId = (String) getSession().get(Constant.SESSION_STREET_ID);
		try {
			themeService.setInUseTheme(streetId, themeId);
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
		}
		return list();
	}
}
